﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using CNVP.JShop.UI;
using CNVP.Config;
using CNVP.Framework.Utils;
using System.Xml;

namespace CNVP.JShop.Web.Admin.Template
{
    public partial class TemplateImport : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Upload1.Save();
            string _FilePath = Upload1.FullPath;
            string _path = Request.Params["path"];
            if (string.IsNullOrEmpty(_path))
            {
                _path = @"~/Template/Default/";
            }
            string BasePath = Server.MapPath(_path);
            if (!Directory.Exists(BasePath))
            {
                Directory.CreateDirectory(BasePath);
            }
            //执行文件解压操作
            using (FileStream fs = File.Open(Server.MapPath(UIConfig.InstallDir + _FilePath), FileMode.Open))
            {
                ZipClass.ExtractZip(fs, BasePath);
            }
            FileUtils.DeleteFile(Server.MapPath(UIConfig.InstallDir + _FilePath));
            //执行文件拷贝操作
            string FormPath = Server.MapPath(UIConfig.InstallDir + "Template/" + UIConfig.TemplateUse + "/Temp");
            string ToPath = Server.MapPath(_path);
            //拷贝相关文件
            UnitImport("Default");
            CopyFolder(_path, FormPath, ToPath);
            //删除原始文件
            FileUtils.DeleteFolder(FormPath);

            MessageUtils.Write("<script language=\"javascript\" type=\"text/javascript\">top.Content.location.reload();top.removeTB();</script>");
        }
        /// <summary>
        /// 递归拷贝文件夹内容
        /// </summary>
        /// <param name="FormPath"></param>
        /// <param name="ToPath"></param>
        public void CopyFolder(string FullPath, string FormPath, string ToPath)
        {
            try
            {
                if (ToPath[ToPath.Length - 1] != Path.DirectorySeparatorChar)
                    ToPath += Path.DirectorySeparatorChar;
                if (!Directory.Exists(ToPath))
                {
                    Directory.CreateDirectory(ToPath);
                }
                string[] fileList = Directory.GetFileSystemEntries(FormPath);
                foreach (string file in fileList)
                {
                    if (Directory.Exists(file))
                    {
                        CopyFolder(FullPath + Path.GetFileName(file) + "/", file, ToPath + Path.GetFileName(file));
                    }
                    else
                    {
                        File.Copy(file, ToPath + Path.GetFileName(file), true);
                    }
                }
            }
            catch
            {
                CNVP.Framework.Helper.LogHelper.Write("复制文件出错", string.Format("源文件{0}到{1}", FormPath, ToPath));
            }
        }

        #region "导入单元"
        /// <summary>
        /// 导入单元
        /// </summary>
        /// <param name="ProjectName"></param>
        /// <returns></returns>
        public void UnitImport(string ProjectName)
        {
            string FileName = HttpContext.Current.Server.MapPath(UIConfig.InstallDir + "Template/" + ProjectName + "/Style.xml");
            if (FileUtils.FileExists(FileName))
            {
                XmlDocument Xml = new XmlDocument();
                Xml.Load(FileName);
                XmlNodeList RootList = Xml.SelectNodes("cnvp/unit");
                foreach (XmlNode Xn in RootList)
                {
                    string _StyleID = Xn.ChildNodes[0].InnerText;
                    string _UnitName = Xn.ChildNodes[1].InnerText;
                    string _UnitHTML = Xn.ChildNodes[2].InnerText;
                    BLL.CNVP_UnitsInfo bll = new BLL.CNVP_UnitsInfo();
                    //删除已经存在的标签
                    Model.CNVP_UnitsInfo model_Del = new Model.CNVP_UnitsInfo();
                    model_Del.UnitName = _UnitName;
                    model_Del.ProjectName = ProjectName;
                    bll.Delete_CNVP_UnitsInfo(model_Del);
                    //再插入导入的标签
                    Model.CNVP_UnitsInfo model_Add = new Model.CNVP_UnitsInfo();
                    model_Add.StyleID = Convert.ToInt32(_StyleID);
                    model_Add.UnitName = _UnitName;
                    model_Add.ProjectName = ProjectName;
                    model_Add.UnitHTML = _UnitHTML;
                    bll.Add_CNVP_UnitsInfo(model_Add);
                }
            }
        }
        #endregion

    }
}